home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_300
/
314_01
/
mnpdrvr.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-05-14
|
22KB
|
889 lines
/*=====================================================================
The Microcom MNP Library
(Microsoft C Version)
10/05/89 - lcb.lt_rsn++ fix in rcv_lt...gp
11/4/87 - explicit data typing...gp
8/27/87 - rcv_lt() modified to use lcb.lcl_credit..gp
-----------------------------------------------------------------------
LDRIVER - MNP Link RS-232 Interrupt Service Routines
This module implements asynchronous I/O for IBM PC-compatible
machines and thus much of this code may not be portable to
other dissimilar environments.
This code implements Class 2 of the MNP link protocol,
i.e. it uses the stop/start byte-oriented framing technique.
A Class 3 implementation is not possible in the IBM PC
environment since the communications hardware is not capable
of switching to synchronous (bit-oriented) operation.
Protocol messages and procedures, however, are independent
of the framing technique.
Global routines:
- ascode: service I/O interrupt
- lne_stat: check carrier status
- trigger_sf: initiate transmit interrupts
=====================================================================*/
/* Header files
*/
#include <dos.h>
#include <mnpdat.h>
/* Interrupt-related definitions
*/
#define INT_8259A_PORT 0x20
#define EOI 0x20
#define RCV_DATA 4
#define THR_EMPTY 2
#define MODSTAT_CHNG 0
/* Send framer state names
*/
#define SF_INIT 0
#define SF_DLE 1
#define SF_STX 2
#define SF_INFO 3
#define SF_DLEINFO 4
#define SF_ETX 5
#define SF_FCS1 6
#define SF_FCS2 7
/* Receive framer state names
*/
#define RF_INIT 0
#define RF_DLE 1
#define RF_STX 2
#define RF_HDLEN 3
#define RF_HEADER 4
#define RF_INFOL 5
#define RF_INFO 6
#define RF_FCS1 7
#define RF_FCS2 8
/* Frame octet definitions
*/
#define SYN 0x16
#define DLE 0x10
#define ETX 3
#define STX 2
/* Length of header buffer (maximum acceptable LPDU header)
*/
#define MAX_HDRLEN 100
/* Bit set, clear and test definitions
*/
#define SETBIT1(bit) lcb.status_1 |= bit;
#define SETBIT2(bit) lcb.status_2 |= bit;
#define SETBIT3(bit) lcb.status_3 |= bit;
#define CLRBIT1(bit) lcb.status_1 &= ~bit;
#define CLRBIT2(bit) lcb.status_2 &= ~bit;
#define CLRBIT3(bit) lcb.status_3 &= ~bit;
#define BIT1SET(bit) (lcb.status_1 & bit)
#define BIT2SET(bit) (lcb.status_2 & bit)
#define BIT3SET(bit) (lcb.status_3 & bit)
/* External references
*/
extern USIGN_16 iir_add; /* int id reg address */
extern USIGN_8 linestat; /* line status var */
extern struct TIMERS tmr;
extern struct link_ctl_blk lcb; /* link control block */
extern struct BUFFER rb, ftb, rlkb; /* buffers */
extern USIGN_8 *rb_iptr; /* rcv buf insert pointer */
extern USIGN_16 rb_cnt,tbcnt; /* buffer counts */
extern USIGN_8 rbuf[RBUF_LEN]; /* receive buffer */
/* Function definitions
*/
void snd_framer();
void rcv_framer();
void mod_stat();
void rcv_lt();
void rcv_la();
void rcv_lna();
void rcv_ln();
/* Local data
*/
USIGN_8 *sf_ptr,
*rf_hptr,
*rf_dptr;
USIGN_8 snd_char,
rcv_char;
struct
{
USIGN_8 low;
USIGN_8 hi;
}
snd_fcs, /* send fcs */
rcv_fcs, /* calculated receive fcs */
rfcs; /* received fcs */
USIGN_16 rdle_flg; /* 'previous char DLE' flag */
USIGN_16 rf_hdrlen, /* receive header length */
hdrcnt,
rf_datlen,
sf_state, /* send framer state var */
rf_state, /* receive framer state var */
sf_len,
sf_busy, /* 'send frame' flag */
sf_lt, /* 'sending LT' flag */
frame_snt, /* 'info field sent' flag */
frame_rcvd, /* 'frame received' flag */
frame_dne; /* 'frame sent' flag */
USIGN_8 *rf_tiptr;
struct MNP_CB *p_mnpcb;
struct BLST *dat_struct, *hdr_struct;
USIGN_16 modem_out_busy; /* RS-232 hardware status flag
0=transmitter not active
1=transmitter active */
/* Function declarations
*/
USIGN_8 get_char();
/*GLOBAL***************************************************************
ascode - MNP RS-232 interrupt service routine
This routine is called by 'async' (in module 'async.asm')
to service an async I/O interrupt.
**********************************************************************/
ascode()
{
USIGN_8 int_id_reg; /* value in int id reg */
/* Handle interrupts for as long as there are any.
*/
for (;;)
{
/* Read the Interrupt Identification Register. If bit 0 is on,
** there is no interrupt. In this case cancel the interrupt by writing ** the "end-of-interrupt" byte to the Oper Ctl Word 2 and return.
*/
if ((int_id_reg = inp(iir_add)) & BIT0_ON)
{
outp(INT_8259A_PORT, EOI);
return;
}
/* Since bit 0 is off, there is an interrupt. Take action based
** on the type of interrupt.
*/
switch (int_id_reg)
{
/* Receive buffer full interrupt
*/
case RCV_DATA:
rcv_framer();
if ((sf_busy == FALSE)
|| ((inp(iir_add + 3) & 0x20) == 0))
break;
/* Transmit buffer empty interrupt
*/
case THR_EMPTY:
snd_framer();
break;
/* Modem status change interrupt
*/
case MODSTAT_CHNG:
mod_stat();
break;
}
}
}
/*GLOBAL***************************************************************
lne_stat - physical-connection status routine
**********************************************************************/
SIGN_16 lne_stat()
{
/* If the carrier detect bit is 1, return success (physical-connection
** still active). Otherwise, return with a failure indication.
*/
if (linestat & 0x80)
return (SUCCESS);
else
return (1);
}
/*GLOBAL***************************************************************
trigger_sf - initiate send framer
**********************************************************************/
void trigger_sf()
{
extern USIGN_16 port_add;
/* If the RS-232 transmitter is not already busy, then a byte must
** be placed in the transmit buffer to begin interrupt-driven sending.
** This is the start of a frame and thus the first byte to be sent is
** the SYN which begins the frame start flag. Also the send framer's
** state variable is initialed here.
*/
if (!modem_out_busy)
{
modem_out_busy = TRUE;
sf_state = SF_DLE;
outp(port_add,SYN);
}
}
/*LOCAL----------------------------------------------------------------
get_char - get byte from send buffer
---------------------------------------------------------------------*/
USIGN_8 get_char()
{
/* Return the byte at the current send framer pointer position.
** In the process, advance the point for next time and reduce the
** number of bytes remaining by one.
*/
sf_len--;
return (*sf_ptr++);
}
/*LOCAL----------------------------------------------------------------
mod_stat - modem status change interrupt service routine
---------------------------------------------------------------------*/
void mod_stat()
{
/* Read modem status register and save the value.
*/
linestat = inp(iir_add + 4);
/* If carrier has been lost, signal the mainline that the link
** also has been lost by lowering the 'link established' flag.
*/
if (!(linestat & 0x80))
CLRBIT1(LINK_EST)
}
/*LOCAL----------------------------------------------------------------
put_char - store an LT data byte
---------------------------------------------------------------------*/
void put_char()
{
/* Ignore this byte if the amount of received data exceeds the
** negotiated maximum user data size for the LT LPDU. This could occur
** if the frame is broken or if the correspondent is misbehaving.
*/
if (rf_datlen >= lcb.max_data_sz)
return;
/* Otherwise, accept this byte.
*/
rf_datlen++;
fcscalc (&rcv_fcs, rcv_char);
/* Now store the data byte in the receive buffer using the temporary
** insert pointer. Advance the pointer, checking for wrap.
*/
*rf_tiptr++ = rcv_char;
if (rf_tiptr >= rbuf + RBUF_LEN)
rf_tiptr = rbuf;
}
/*LOCAL-------